package com.example.clooweather.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;

import com.example.clooweather.gson.WeatherBean;
import com.example.clooweather.util.HttpUtil;
import com.example.clooweather.util.Utility;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AutoUpdateService extends Service {
    public AutoUpdateService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        updateWeather();
        updateBinPic();
        AlarmManager manager=(AlarmManager)getSystemService(ALARM_SERVICE);
        //AlarmManager一种系统级别的提示服务
        int anHour= 4 * 60 * 60 * 1000;//4个小时的毫秒数
        long triggerAttime= SystemClock.elapsedRealtime()+anHour;
        //SystemClock.elapsedRealtime()获取从设备开机后经历的时间值。
        Intent i=new Intent(this,AutoUpdateService.class);
        PendingIntent pi=PendingIntent.getService(this,0,i,0);
        manager.cancel(pi);
        //
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAttime,pi);
        //manager的set方法第一个整形参数ELAPSED_REALTIME_WAKEUP代表定时任务触发时间从系统开计算起会唤醒CPU
        //ELAPSED_REALTIME表示代表定时任务触发时间从系统开计算起但是不会唤醒CPU
        //第二个参数是定时任务触发的时间 第三个参数PendingIntent
        return super.onStartCommand(intent, flags, startId);
    }
    private void updateWeather() {
        SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
        String weatherString=prefs.getString("weather",null);
        if (weatherString!=null){
            Gson gson=new Gson();
            WeatherBean weatherBean=gson.fromJson(weatherString, WeatherBean.class);
            List<WeatherBean.HeWeather6Bean> weatherBeans=weatherBean.getHeWeather6();
            WeatherBean.HeWeather6Bean heWeather6Bean=weatherBeans.get(0);
            WeatherBean.HeWeather6Bean.BasicBean basicBean=heWeather6Bean.getBasic();
            String weatherId=basicBean.getCid();
            String weatherUrl="https://free-api.heweather.com/s6/weather?location="+weatherId+"&key=dde9e47ec0f74c1297849627274108a7";
           //http://guolin.tech/api/weather?cityid=CN101020300&key=dde9e47ec0f74c1297849627274108a7
            HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String reponseText=response.body().string();
                    Gson gson=new Gson();
                    final WeatherBean weatherBean=gson.fromJson(reponseText, WeatherBean.class);
                    final List<WeatherBean.HeWeather6Bean> weatherBeans=weatherBean.getHeWeather6();
                    if (weatherBeans != null&&weatherBeans.size()>0){
                        SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
                        editor.putString("weather",reponseText);
                        editor.apply();
                    }
                }
            });
        }
    }
    private void updateBinPic() {
        String requestBinPic="http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkHttpRequest(requestBinPic, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String bingPic=response.body().string();
                //将返回的数据缓存到SharedPreferences中
                SharedPreferences.Editor editor=PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
                editor.putString("bing_pic",bingPic);
                //向editor.putString添加数据
                editor.apply();
                //提交数据 editor.apply();
            }
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
        });

    }


}
